package cn.edu.ujn.lab3.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.ujn.lab3.dao.User;
import cn.edu.ujn.lab3.dao.UserMapper;
import cn.edu.ujn.lab3.service.UserService;
import cn.edu.ujn.lab3.util.Page;


@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
	// 注入UserDao
	@Autowired
	private UserMapper userDao;
	// 通过账号和密码查询用户
	@Override
	public User findUser(String usercode, String password) {
		User user = this.userDao.findUser(usercode, password);
		return user;
	}
	//查询User的列表
	@Override
	public Page<User> findUserList(Integer page, Integer rows, String userCode, String userName, Integer userState) {
		User user = new User();
		if (StringUtils.isNotBlank(userCode)) {
			user.setUser_code(userCode);
		}
		if (StringUtils.isNotBlank(userName)) {
			user.setUser_name(userName);
		}
		
		user.setUser_state(userState);
		user.setStart((page-1)*rows);
		user.setRows(rows);

		List<User> userList = userDao.selectUserList(user);
		Integer count = userDao.selectUserListCount(user);
		Page<User> result = new Page<User>();
		result.setPage(page);
		result.setRows(userList);
		result.setSize(rows);
		result.setTotal(count);
		return result;
	}
	//创建用户
	@Override
	public int createUser(User user) {
		return userDao.insert(user);
	}
	//通过id查询用户
	@Override
	public User getUserById(Integer id) {
		return userDao.selectByPrimaryKey(id);
	}
	//更新用户
	@Override
	public int updateUser(User user) {
		return userDao.updateByPrimaryKeySelective(user);
	}
	
	//删除用户
	@Override
	public int deleteUser(Integer id) {
		return userDao.deleteByPrimaryKey(id);
	}
}
